package com.ybwh.springboot2.exmple.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ybwh.springboot2.common.mybatis.plus.update.force.MybatisPlusForceUpdater;
import com.ybwh.springboot2.common.mybatis.plus.update.force.UpdateEntity;
import com.ybwh.springboot2.exmple.entity.Dict;
import com.ybwh.springboot2.exmple.mapper.DictMapper;
import com.ybwh.springboot2.exmple.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author fanbeibei
 * @since 2023-07-12
 */
@Service
public class DictServiceImpl implements DictService {
    @Resource
    private DictMapper dictMapper;

    @Autowired
    private MybatisPlusForceUpdater mybatisPlusForceUpdater;

    /**
     * 之所以要生成以下方法而不直接用mybatis-plus的原始方法，是为了方便追溯某个mapper调用代码,
     * 避免find use后找出一批无关的代码
     */

    @Override
    public int insert(Dict entity) {
        return dictMapper.insert(entity);
    }

    @Override
    public int deleteById(Serializable id) {
        return dictMapper.deleteById(id);
    }

    @Override
    public int deleteBatchIds(Collection<? extends Serializable> idList) {
        return dictMapper.deleteBatchIds(idList);
    }

    @Override
    public int updateById(Dict entity) {
        return dictMapper.updateById(entity);
    }

    @Override
    public Dict selectById(Serializable id) {
        return dictMapper.selectById(id);
    }

    @Override
    public List<Dict> selectBatchIds(Collection<? extends Serializable> idList0) {

        List<? extends Serializable> idList = new ArrayList<>(idList0);

        final int BATCH_SIZE = 1000;

        LambdaQueryWrapper<Dict> query = new LambdaQueryWrapper<>();


        if (idList.size() <= BATCH_SIZE) {
            query.in(Dict::getId, idList);
        } else {
            query.nested(q -> {
                for (int i = 0; i < idList.size(); i += BATCH_SIZE) {
                    int end = Math.min(i + BATCH_SIZE, idList.size());
                    List<? extends Serializable> batch = idList.subList(i, end);
                    q.or().in(Dict::getId, batch);
                }
            });
        }


        return dictMapper.selectList(query);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void forceUpdateTransaction() {


        Dict dict = UpdateEntity.of(Dict.class, 1L);
        dict.setDictDescr("33333");
        dict.setDictGroup(null);
        forceUpdateById(dict);


        Dict dict2 = new Dict();
        dict2.setDictDescr("33333");
        dict2.setDictGroup("rtyryrryrr");

        dictMapper.insert(dict2);

//        throw new RuntimeException();

    }


    @Override
    public void forceUpdateById(Dict dict) {
        mybatisPlusForceUpdater.forceUpdateById(dict);
    }


}
